/**
 * @author v.lugovsky
 * created on 16.12.2015
 */
(function () {
    'use strict';

    angular.module('BlurAdmin.pages.class')
        .controller('ClassManageCtrl', ClassManageCtrl);

    /** @ngInject */
    function ClassManageCtrl($scope,ClassModel,id,AppUtils,$uibModal,$state,toastr) {
        var vm = this;
        vm.schoolClass = {};
        vm.page = {};

        //初始化班级学生列表分页
        vm.initPage = function(){
            ClassModel.getStudentList({
                classId: id,
                pageInfo: {
                    pageSize: 10,
                    pageNumber: vm.page.pageIndex
                }
            }).$promise.then(function(response){
                var result = response.pageResult;
                vm.page = _.clone(result);
            });
        }
        //初始化班级教师信息
        vm.initTeacherInfo = function(){
            ClassModel.getClassTeacher({id:id}).$promise.then(function(response){
                if(response.stat == 'err'){
                    AppUtils.alert(response.msg);
                }else{
                    vm.schoolClass = response.schoolClass;
                }
            })
        }

        //跳完班级信息修改
        $scope.modifyButton = function(){
            $state.go("class.save",{id:id});
        }

        //打开添加学生弹出层
        $scope.open = function () {
            var addStudentMode = $uibModal.open({
                animation: true,
                templateUrl: 'app/pages/class/list/class-addStudent.html',
                windowClass: 'xx-dialog',
                size: 'lg',
                controller: 'ClassAddManageCtrl as vm',
                resolve: {
                    id: function ($stateParams) {
                        return $stateParams.id;
                    }
                }
            });
            //添加完学生之后，关闭弹出层重新加载学生列表
            addStudentMode.result.then(function (selectedItem) {}, function () {
                vm.initPage();
            });
        };

        $scope.gobak = function(){AppUtils.goBack();}


        //删除学生
        $scope.deleteSt = function(memberClassId,$index){
            ClassModel.deleteStudent({id:memberClassId}).$promise.then(function(response){
                if(response.stat == "success"){
                    toastr.success('删除成功！');
                    vm.page.pageResult.splice($index,1);
                }else if(response.stat == "err"){
                    AppUtils.alert("删除失败！");
                }
            })
        }


        vm.initTeacherInfo();
        vm.initPage();
    }

})();
